package com.smarthome.core.account.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShAccountTypeDao;
import com.smarthome.base.service.ShAccountTypeResourceService;
import com.smarthome.base.service.ShAccountTypeService;
import com.smarthome.core.account.AccountTypeService;
import com.smarthome.domain.bean.ShAccountType;
import com.smarthome.domain.bean.ShAccountTypeResource;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
* @author zyf
* @date 2015年9月14日 下午9:41:58
* @version 1.0
* @Description: 平台用户类型服务类
 */
@Service("accountTypeService")
public class AccountTypeServiceImpl implements AccountTypeService {
	
	@Resource
	private ShAccountTypeService shAccountTypeService;
	
	@Resource
	private ShAccountTypeDao shAccountTypeDao;
	
	@Resource
	private ShAccountTypeResourceService shAccountTypeResourceService;
	
	@Override
	public DataGrid queryDataGrid(ShAccountType accountType, PageHelper page) {
		return shAccountTypeService.queryDataGrid(accountType, page);
	}

	@Override
	public List<ShAccountType> queryAccountTypesByCondition(
			ShAccountType accountType) {
		return shAccountTypeService.selectEntryList(accountType);
	}

	@Override
	public ShAccountType queryAccountTypeById(Long id) {
		return shAccountTypeService.selectEntryById(id);
	}

	@Override
	public void add(ShAccountType accountType,Long[] features) {
		//新增平台用户类型
		shAccountTypeService.saveOrUpdate(accountType);
		Long typeId=Long.parseLong(shAccountTypeDao.lastSequence()+"");
		
		//新增平台用户类型和功能中间表数据
		for(Long featureId:features){
			ShAccountTypeResource adds=new ShAccountTypeResource();
			adds.setTypeId(typeId);
			adds.setFeatureId(featureId);
			shAccountTypeResourceService.saveOrUpdate(adds);
		}
	}

	@Override
	public void edit(ShAccountType accountType,Long[] features) {
		//删除平台用户类型对应的功能;
		ShAccountTypeResource satr=new ShAccountTypeResource();
		satr.setTypeId(accountType.getId());
		shAccountTypeResourceService.deleteByCondtion(satr);
		
		//修改平台用户类型
		shAccountTypeService.saveOrUpdate(accountType);
		
		//新增平台用户类型和功能中间表数据
		for(Long featureId:features){
			ShAccountTypeResource adds=new ShAccountTypeResource();
			adds.setTypeId(accountType.getId());
			adds.setFeatureId(featureId);
			shAccountTypeResourceService.saveOrUpdate(adds);
		}
	}

	@Override
	public void delete(Long id) {
		//删除平台用户类型对应的功能;
		ShAccountTypeResource satr=new ShAccountTypeResource();
		satr.setTypeId(id);
		shAccountTypeResourceService.deleteByCondtion(satr);
		
		//删除平台用户类型
		shAccountTypeService.deleteByIds(id);
	}

	@Override
	public List<ShAccountTypeResource> queryTypeFeaturesByTypeId(Long typeId) {
		ShAccountTypeResource satr=new ShAccountTypeResource();
		satr.setTypeId(typeId);
		return shAccountTypeResourceService.selectEntryList(satr);
	}

	@Override
	public List<Combobox> getCombobox() {
		List<ShAccountType> res = shAccountTypeService.selectEntryList(null);
		List<Combobox> result = new ArrayList<Combobox>();
		for(ShAccountType type:res){
			Combobox combo=null;
			if(type.getName().contains("一般")||type.getName().contains("默认")){
				combo = new Combobox(type.getId()+"",type.getName(),true);
			}else{
				combo = new Combobox(type.getId()+"",type.getName());
			}
			result.add(combo);
		}
		return result;
	}

}
